//index.js
//获取应用实例
const app = getApp()
let canvas = require('../../common/js/canvas.js');
let Axios = require('../../common/js/ajax.js');
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 1, //当前导航
		height: 866,
		top: 0,
		imgHeight: 0,
		hcImg: '',
		tips: false,
		shareTk: false,
		canvasTk: false,
		followTk: false,
		repTk: false,
		repTkCon: '',
		videoPlay: false,
		otherVideoPlay: false,
		goods_id: 0,
		good: {},
		evaluate: [],
		cur_video:'',
		showImg:[],
		isIpx:app.globalData.isIpx?true:false,
		showImg:[]
	},
	//事件处理函数
	onLoad: function(e) {
		this.setData({
			goods_id: e.id
		})
		this.load = this.selectComponent("#loading");
		this.login = this.selectComponent("#login");
		var _this = this;
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Goods/get_goods_detail', { //关键字
			goods_id: this.data.goods_id,
			uid: this.uid
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					good: data
				})			
				_this.load.loadIng()
			}else{
				_this.showModal(res)
			}
		})
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Goods/get_goods_evaluate', { //关键字
			goods_id: this.data.goods_id,
			uid: this.uid
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					evaluate: data
				})
			}
		})
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '宝贝详情'
		})
	},
	preventTouchMove: function() {

	},
	bubbling: function() {

	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	videoPlay: function(e) { //视频播放
		var id = e.currentTarget.dataset.id
		console.log(id)
		this.setData({
			cur_video: id,
			vpause: false
		})
		this.videoContext = wx.createVideoContext('myVideo' + id)
		this.videoContext.requestFullScreen({direction: 0});
		this.videoContext.play();
	},
	tapPause:function(){
		this.videoContext.pause();
	},
	videoPause: function() { //监听视频暂停
		if (this.data.vpause) {
			this.setData({
				cur_video: '',
			})
		}
	},
	vwait: function() { //判断视频是否加载完成
		this.setData({
			vpause: true,
		})
	},
	handleCloseTap: function() {
		this.setData({
			followTk: false, //取消关注		
			canvasTk: false, //合成图片		
			shareTk: false,
			repTk: false,
			height: 0,
			tips: false
		})
	},
	handleRepTap: function(e) { //举报
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		var id = e.currentTarget.dataset.id;
		this.setData({
			repTk: true,
		})
	},
	bindAreaBlur: function(e) { //焦点
		var rep = e.detail.value
		this.setData({
			repTkCon: rep
		})
	},
	handleRepTkTap: function(e) { //提交举报
		var _this = this;
		var id = this.data.good.id
		if (this.data.repTkCon != '') {
			Axios.post(app.globalData.baseUrl + 'App/Store/Goods/goods_report', {
				uid: this.uid,
				goods_id: id,
				remark: _this.data.repTkCon,
				time: _this.timesp,
				sign: _this.Md5
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					_this.setData({
						tips: true,
						'good.is_report': 1
					})
				}else{
					_this.showModal(res)
				}
			})
		} else {
			wx.showToast({
				title: '内容不能为空',
				image: '/img/warn-icon.png',
				duration: 1000,
				mask: true
			})
		}
	},
	handleFollowTap: function(e) { //关注
		var _this = this;
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		var id = _this.data.good.store_info.id
		var follow = _this.data.good.store_info.is_follow
		if (follow == 1) {
			_this.setData({
				followTk: true,
			})
		} else {
			Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
				store_id: id,
				uid: this.uid,
				sign: this.Md5,
				time: this.timesp
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					_this.setData({
						['good.store_info.is_follow']: 1
					})

				}
			})
		}
	},
	handleCancelFoTap: function(e) { //取消关注
		var _this = this;
		var id = _this.data.good.store_info.id
		var follow = _this.data.good.store_info.is_follow
		if (follow == 1) {
			Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
				store_id: id,
				uid: this.uid,
				sign: this.Md5,
				time: this.timesp
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					_this.setData({
						['good.store_info.is_follow']: 0,
						followTk: false
					})
				}else{
					_this.showModal(res)
				}
			})
		}

	},
	handleZanTap: function() { //赞	
		var _this = this
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		Axios.get(app.globalData.baseUrl + 'App/Store/Goods/goods_fabulous', { //关键字
			goods_id: this.data.goods_id,
			uid: this.uid,
			sign: this.Md5,
			time: this.timesp
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				this.setData({
					'good.is_fabulous': data.is_fabulous
				})
				Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Goods/get_goods_detail', { //关键字
					goods_id: this.data.goods_id,
					uid: this.uid
				}, (res) => {
					console.log(res)
					if (res.status == 1) {
						var data = res.info
						_this.setData({
							good: data
						})
					}else{
						_this.showModal(res)
					}
				})
			}else{
				_this.showModal(res)
			}
		})
	},
	handleShareTap: function(e) { //分享弹窗		
		var _this = this;
		this.setData({
			shareTk: true,
		})
	},
	handleBuyTap: function(e) {
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		var id = this.data.good.id;
		if (this.data.good.stock > 0  ) {
			if(this.data.good.is_report == 0){
				wx.navigateTo({
					url: "/pages/order/order?goods_id=" + id,
				})			
			}else{
				wx.showModal({
					content: '商品已经被举报',
					showCancel: false,
				})
			}
			
		} else {
			wx.showModal({
				content: '库存不够',
				showCancel: false,
			})
		}
	},
	handleSxTap: function() {
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
	},
	handleCanvasTap: function(e) { //合成图片				
		var id = e.currentTarget.dataset.id;
		var HEIGHT = 670;
		var _this = this;
		var picinit = 0;
		var logoinit = 0;
		var textH = canvas.heightComputed(this.data.good.share_content, 510, 28, 37)
		picinit = textH
		textH += canvas.heightComputed(this.data.good.title, 560, 28, 37)
		logoinit = textH
		console.log(_this.data.nickName)
		var fontWith = canvas.widthComputed(_this.data.nickName, 48)
		console.log(fontWith)
		this.setData({
			height: (textH + HEIGHT),
			imgHeight: (textH + HEIGHT),
			//top: e.detail.y
		})
		wx.showLoading({
			title: '绘图中···',
		})
		var avatarUrl = '';
		var temp_picture = '';		
		var wxEwm = '';//二维码
		Axios.post(app.globalData.baseUrl + 'App/PublicCommon/Share/share_img', {
			object_id:id,
			type:2
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info		
				wx.downloadFile({
					url: app.globalData.baseUrl + data,
					success: function(res) {	
						wxEwm = res.tempFilePath
						wx.downloadFile({
							url: _this.data.avatarUrl,
							success: function(res) {
								avatarUrl = res.tempFilePath
								if (_this.data.good.master_picture.length > 0) {
									wx.downloadFile({
										url:  app.globalData.baseUrl +_this.data.good.master_picture[0],
										success: function(res) {
											temp_picture = res.tempFilePath
											console.log(333)
											draws()
										},
										fail: function(fres) {
											wx.hideLoading();
										}
									})
								} else {
									draws()
								}
							},
							fail: function(fres) {
								wx.hideLoading();
							}
						})
					},
					fail: function(fres) {
						wx.hideLoading();
					}
				})							
			} else {
				wx.hideLoading();
				_this.showModal(res)
			}
		})
		function draws() {
			app.ctx.setFillStyle('white')
			app.ctx.fillRect(0, 0, 650, (textH + HEIGHT) * .552)
			console.log(_this.data.avatarUrl)
			var avatar = {
				src: avatarUrl,
				x: 32,
				y: 53,
				width: 88,
				height: 88
			}
			canvas.drawAvatar(avatar)
			var proImg = {
				src: temp_picture,
				x: 178,
				y: 118 + picinit,
				width: 285,
				height: 285
			}
			canvas.drawImg(proImg)
			var canvasLogo = {
				src: '/img/canvasLogo.png',
				x: 43,
				y: 498 + logoinit,
				width: 333,
				height: 39
			}
			canvas.drawImg(canvasLogo)
			var author = {
				x: 139,
				y: 70,
				color: '#006e88',
				size: 28,
				align: 'left',
				baseline: 'middle',
				text: _this.data.nickName,
				bold: true
			}
			canvas.drawText(author)
			var sign = {
				x: 158 + fontWith,
				y: 70,
				color: '#f08415',
				size: 28,
				align: 'left',
				baseline: 'middle',
				text: '推荐',
				bold: true
			}
			canvas.drawText(sign)
			var adverter = {
				x: 139,
				y: 118,
				width: 510,
				height: 34,
				line: 2,
				color: '#3f4446',
				size: 28,
				align: 'left',
				baseline: 'middle',
				bold: false,
				text: _this.data.good.share_content
			}
			canvas.textWrap(adverter)
			var desc = {
				x: 40,
				y: 455 + picinit,
				width: 560,
				height: 34,
				line: 2,
				color: '#3f4446',
				size: 28,
				align: 'left',
				baseline: 'middle',
				bold: false,
				text: _this.data.good.title
			}
			canvas.textWrap(desc)
			var wxLogo = {
				src: wxEwm,
				x: 468,
				y: 455 + logoinit,
				width: 138,
				height: 138
			}
			var avatar2 = {
				src: avatarUrl,
				x: 506,
				y: 490 + logoinit,
				width: 68,
				height: 68
			}					
			canvas.drawImg(wxLogo)
			canvas.drawAvatar(avatar2)
			app.ctx.draw()
			setTimeout(function() {
				wx.canvasToTempFilePath({
					x: 0,
					y: 0,
					canvasId: 'firstCanvas',
					success: function(res) {
						wx.hideLoading();
						// 设置画布高度						
						_this.setData({
							hcImg: res.tempFilePath,
							canvasTk: true, //合成图片
							//tk: true,
							height: 0
						})
					}
				});
			}, 1000)
		}
	},
	handleSaveTap: function() { //保存到相册
		console.log('保存到相册')
		wx.getImageInfo({
			src: this.data.hcImg, //
			success: function(res) {
				wx.saveImageToPhotosAlbum({
					filePath: res.path,
					success: function(dres) {
						wx.showToast({
							title: '已保存到相册'
						})
					}
				})
			}
		})
	},
	onShareAppMessage: function() {
		let _this = this;
		var id = this.data.good.id;
		this.setData({
			shareTk: false,
		})
		return {
			title: _this.data.good.share_content, // 转发后 所显示的title
			path: '/pages/goodDetail/goodDetail?id=' + id, // 相对的路径
			success: function(res) { // 成功后要做的事情
				console.log(res.shareTickets[0])
				// console.log
				wx.getShareInfo({
					shareTicket: res.shareTickets[0],
					success: function(res) {

					},
					fail: function(res) {
						console.log(res)
					},
					complete: function(res) {
						console.log(res)
					}
				})
			},
			fail: function(res) {
				// 分享失败
				console.log(res)
			}
		}
	},
	previewImage: function (e) {
    var current = e.currentTarget.dataset.src;
		var type = e.currentTarget.dataset.type
		var temp = [];
		if(type == 0){
			this.data.good.master_picture.forEach(function(item,key){
				temp.push(app.globalData.baseUrl + item)
			})
		}else if(type == 1){
			this.data.good.sub_picture.forEach(function(item,key){
				temp.push(app.globalData.baseUrl + item)
			})
		}else{
			var index = e.currentTarget.dataset.index
			this.data.evaluate[index].picture.forEach(function(item,key){
				temp.push(app.globalData.baseUrl + item)
			})
		}			
		this.setData({
			showImg: temp
		})
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.showImg // 需要预览的图片http链接列表  
    })
  },
	handlelettertap:function(e){
		let user = wx.getStorageSync('userInfo') || {}
		var id = e.currentTarget.dataset.id;
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		if(id == user.identifier){
			wx.showModal({
				content: '不能和自己聊天',
				showCancel: false,
			})
			return false
		}		
		wx.navigateTo({
			url:'/pages/center/chatRoom/chatRoom?id='+id
		})
	}
})
